package cn.zebra.dev.mall.module.category.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.mall.module.category.dto.CateGorySecondDTO;
import cn.zebra.dev.mall.module.category.dto.CateGoryThirdDTO;
import cn.zebra.dev.mall.module.category.dto.CategoryDTO;
import cn.zebra.dev.product.module.category.constant.CategoryTypeConstant;
import cn.zebra.dev.product.module.category.entity.CategoryEntity;
import cn.zebra.dev.product.module.category.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "商品分类")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("商品分类列表")
    public Result<PageUtils<CategoryDTO>> list(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("limit", limit);
        params.put("page", page);
        params.put("categoryType", CategoryTypeConstant.PRODUCT);
        params.put("parentId",0); // 查询一级分类
        PageUtils<CategoryEntity> pageUtils = categoryService.findPage(params);
        List<CategoryEntity> list = pageUtils.getList();
        PageUtils<CategoryDTO> data = new PageUtils<>(pageUtils);
        if (list.size() > 0) {
            List<CategoryDTO> dtoList = new ArrayList<>();
            //得到一级分类的id组
            List<Long> parentId = list.stream().map(CategoryEntity::getId).collect(Collectors.toList());
            //得到二级分类的全部对象
            List<CategoryEntity> categorySecondEntities = categoryService.findAllByParentIds(parentId);
            //得到二级分类的id组
            List<Long> secondParentIds = categorySecondEntities.stream().map(CategoryEntity::getId).collect(Collectors.toList());
            //得到三级分类的全部对象
            List<CategoryEntity> categoryThirdEntities = categoryService.findAllByParentIds(secondParentIds);
            list.forEach(entity -> {
                CategoryDTO dto = new CategoryDTO();
                BeanUtils.copyProperties(entity, dto);
                List<CategoryEntity> categorySecondEntity = categorySecondEntities.stream()
                        .filter(categoryEntity -> categoryEntity.getParentId().equals(entity.getId())).collect(Collectors.toList());
                List<CateGorySecondDTO> cateGorySecondDTO = new LinkedList<>();
                categorySecondEntity.forEach(entitySecond ->{
                    CateGorySecondDTO cateGorySonDTOs = new CateGorySecondDTO();
                    BeanUtils.copyProperties(entitySecond,cateGorySonDTOs);
                    List<CategoryEntity> categoryThirdEntity = categoryThirdEntities.stream()
                            .filter(categoryEntity -> categoryEntity.getParentId().equals(entitySecond.getId())).collect(Collectors.toList());
                    List<CateGoryThirdDTO> cateGoryThirdDTOs = new LinkedList<>();
                    categoryThirdEntity.forEach(entityThird -> {
                        CateGoryThirdDTO cateGoryThirdDTO = new CateGoryThirdDTO();
                        BeanUtils.copyProperties(entityThird,cateGoryThirdDTO);
                        cateGoryThirdDTOs.add(cateGoryThirdDTO);
                        cateGorySonDTOs.setCateGoryThirdDTOS(cateGoryThirdDTOs);
                    });
                    cateGorySecondDTO.add(cateGorySonDTOs);
                });
                dto.setCateGorySonDTO(cateGorySecondDTO);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }
}
